import redi_utils as utils
import redi_sqldb as sqldb

def load(dataset_id, filename, config, options):
    itemid = utils.get_filename(filename)
    if not options.id is None: itemid = options.id

    outfile = "REFERENCES_" + itemid
    utils.copy_file(dataset_id + "/references/" + outfile, filename)

    schema = []

    rfile = open(filename)
    columnheaders = rfile.next()
    for column in columnheaders.rstrip().split("\t"):
        columnName = column.rstrip().replace(" ", "_")
        schema.append({ "name": columnName, "label": column, "type": "string" });

    return { "id": itemid, "type":"references", "table": outfile, "schema": schema }

def load_old(dataset_id, filename, config, options):
    columnssql = ""

    headerfile = options.headerFile
    if headerfile is None:
        rfile = open(filename)
        columnheaders = rfile.next()
        if columnheaders != "":
            columnlist = columnheaders.rstrip().split("\t")
            for i, column in enumerate(columnlist):
                if i > 0: columnssql += ", "
                columnssql += column.rstrip().replace(" ", "_") + " VARCHAR(100) "
    else:
        rfile = open(headerfile)
        startreading = False
        firstcolumn = True
        for line in rfile.readlines():
            if startreading:
                columnlist = line.rstrip().split("\t")
                if not firstcolumn: columnssql += ", "
                columnssql += columnlist[1].rstrip().replace(" ", "_") + " TEXT "
                firstcolumn = False
            elif "Column Header" in line:
                startreading = True

    itemid = utils.get_filename(filename)
    if not options.id is None: itemid = options.id

    tablename = "REFERENCES_" + itemid

    sqlcmds = []
    sqlcmds.append("CREATE TABLE " + tablename + " (" + columnssql + ")")
    sqlcmds.append("LOAD DATA LOCAL INFILE '" + filename + "' INTO TABLE " + tablename + " FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n' IGNORE 1 LINES")
    sqldb.execute(dataset_id, sqlcmds, config, options)

    return { "table": tablename }
